package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.constant.Url;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.OSUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.activity.BuyCoinsMarketActivity;
import com.qire.manhua.databinding.ActivityBuyCoinsMarketBinding;
import com.qire.manhua.databinding.LayoutBuyCoinsMarketItemBinding;
import com.qire.manhua.dialog.PayWaySelectorOfBuyCoinDialog;
import com.qire.manhua.dialog.TeensPasswordAuthorizeDialog;
import com.qire.manhua.model.entity.MarketInfo.WaresEntity;
import com.qire.manhua.model.enumeration.PaymentMethod;
import com.qire.manhua.model.enumeration.TeensModeScene;
import com.qire.manhua.model.viewModel.impl.BuyCoinsMarketViewModel;
import com.qire.manhua.model.viewModel.impl.BuyCoinsMarketViewModel.WaresItemViewModel;

import androidx.annotation.NonNull;

public class BuyCoinsMarketController extends EventController<BuyCoinsMarketActivity> implements AutoVariable<LayoutBuyCoinsMarketItemBinding> {

    private BuyCoinsMarketViewModel viewModel;

    public BuyCoinsMarketController(@NonNull BuyCoinsMarketActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityBuyCoinsMarketBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setBuyCoinsMarketController(this);
    }


    @Override
    public void bindVariable(LayoutBuyCoinsMarketItemBinding viewBind) {
        viewBind.setBuyCoinsMarketController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BuyCoinsMarketViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**
     * 返回上一页单击事件
     * @param view
     */
    public void backBtnOnclick(View view){
        owner.onBackPressed();
    }

    /**
     * 商品选项单击事件
     * @param view
     */
    public void waresLayoutOnClick(View view){
        WaresItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), WaresItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.toggle();
        }
    }

    /**
     * 立即支付按钮单击事件
     * 1.向服务器提交创建订单，等待placeOrderApiCallbackRefresh()响应
     * @param view
     */
    public void paymentBtnOnclick(View view) {
        if(TeensModeManage.teensModeEnable()) {
            new TeensPasswordAuthorizeDialog(view.getContext(), TeensModeScene.Recharge, this::onPayment).show();
            return;
        }
        onPayment();
    }

    private void onPayment() {
        final WaresEntity waresEntity = viewModel.selectedWares();

        if (ObjectUtil.isNull(waresEntity)) {
            ToastHelper.showSnackBar("请先选择充值金额", Prompt.WARNING);
            return;
        }

        new PayWaySelectorOfBuyCoinDialog(owner, this::paymentMethodOnClick)
                .setWaresFee(String.valueOf(waresEntity.fee))
                .setWaresDescribe(waresEntity.getDescribe())
                .show();
    }

    public void paymentMethodOnClick(View view) {
        PaymentMethod paymentMethod = ObjectUtil.as(view.getTag(), PaymentMethod.class);
        if(ObjectUtil.nonNull(paymentMethod)) {
            viewModel.createOrder(paymentMethod, owner.getClass().getSimpleName());
        }
    }

    /**
     * 找朋友代付单击事件
     * @param view
     */
    public void friendPaidBtnOnClick(View view) {
        FloorCard floorCard = AntsRouter.ROUTER.buildCard(Navigation.Page.FriendPaidPage);
        if(TeensModeManage.teensModeEnable()) {
            new TeensPasswordAuthorizeDialog(view.getContext(), TeensModeScene.Recharge, floorCard::navigation).show();
            return;
        }
        floorCard.navigation();
    }

    /**
     * 联系客服单击事件
     * @param view
     */
    public void launchQQBtnOnclick(View view) {
        OSUtil.launchQQ(owner, viewModel.qqNumber());
//        OSUtil.launchQQ(owner, DeviceConfigInfo.qqNumber());
    }

    /**
     * 奇热漫画充值服务协议单击事件
     * @param view
     */
    public void serviceAgreementBtnOnClick(View view) {
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                .withData("url", Url.rechargeProtocol_url)
                .withData("title", "奇热漫画充值服务协议")
                .navigation();
    }

}
